<template>
    <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
        <el-form class="tl-form" ref="dealform" :model="model" :rules="rules" label-width="120px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="风险等级" prop="riskrank" :rules="rules.validateRequire">
                        <el-select v-model="model.riskrank" filterable clearable>
                            <el-option v-for="item in codeModel.riskrankList" :key="item.code" :label="item.name" :value="item.code" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="风险状态" prop="riskstate" :rules="rules.validateRequire">
                        <el-select v-model="model.riskstate" filterable clearable>
                            <el-option v-for="item in codeModel.riskstateList" :key="item.code" :label="item.name" :value="item.code" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="发生概率" prop="probability" :rules="rules.validateRequire">
                        <el-select v-model="model.probability" filterable clearable>
                            <el-option v-for="item in codeModel.probabilityList" :key="item.code" :label="item.name" :value="item.code" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="影响程度" prop="influencedegree" :rules="rules.validateRequire">
                        <el-select v-model="model.influencedegree" filterable clearable>
                            <el-option v-for="item in codeModel.influencedegreeList" :key="item.code" :label="item.name" :value="item.code" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="建议措施" prop="measures" :rules="rules.validateRequire">
                        <el-input v-model="model.measures" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="120px" :disabled="disableEditSubmit || disableSave">
            <el-row>
                <el-col :span="24">
                    <el-divider content-position="center"><span>【以下信息为只读信息】</span></el-divider>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-divider content-position="left"><span>【风险识别】相关信息</span></el-divider>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="项目" prop="projectid" :required="true" :rules="rules.validateRequire">
                        <el-input v-model="model.dispprojectid" :disabled="true" class="tl-ref-selector">
                            <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="识别日期" prop="findtime">
                        <el-date-picker v-model="model.findtime" type="date" placeholder="识别日期" value-format="yyyy-MM-dd"></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="风险类型" prop="risktype">
                        <el-select v-model="model.risktype" filterable clearable>
                            <el-option v-for="item in codeModel.risktypeList" :key="item.code" :label="item.name" :value="item.code" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="风险名称" prop="riskname">
                        <el-input v-model="model.riskname"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="识别人" prop="identifyusername" :required="true" :rules="rules.validateRequire">
                        <el-input v-model="model.identifyusername" :disabled="true" class="tl-ref-selector">
                            <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmTeamRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="识别人工号" prop="identifyusercode">
                        <el-input v-model="model.identifyusercode"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="风险描述" prop="riskdescribe">
                        <el-input v-model="model.riskdescribe" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="风险分析人" prop="analysisusername" :required="true" :rules="rules.validateRequire">
                        <el-input v-model="model.analysisusername" :disabled="true" class="tl-ref-selector">
                            <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmTeamRefDialogAnalysis" v-if="!disableEditSubmit && !disableSave"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="风险分析人工号" prop="analysisusercode">
                        <el-input v-model="model.analysisusercode"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="备注" prop="description">
                        <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-collapse>
                <el-collapse-item title="【单据】相关信息">
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="申请部门" prop="deptname" :required="true" :rules="rules.validateRequire">
                                <tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.deptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="申请公司" prop="orgname">
                                <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="申请人姓名" prop="username" :required="true" :rules="rules.validateRequire">
                                <tl-ref-user v-model="model.username" :initDisplayData.sync="model.username" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="申请人工号" prop="usercode">
                                <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="单据日期" prop="docdate">
                                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="提交时间" prop="submitdate">
                                <el-date-picker v-model="model.submitdate" type="datetime" placeholder="提交时间" disabled></el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="审批流程" prop="flowid">
                                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="状态" prop="state">
                                <el-input v-model="model.dispstate" disabled></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-collapse-item>
            </el-collapse>
        </el-form>
        <tl-approval-history ref="history" sourceType="PmRiskIdentify" :sourceId="model.id" />
        <div slot="footer" class="dialog-footer">
            <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit" v-if="uiOperateState<=2">取消</el-button>
            <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave">
                <svg-icon icon-class="save" />保存</el-button>
            <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
                <svg-icon icon-class="send" />提交审批</el-button>
            <tl-attachment sourceType="PmRiskIdentify" :sourceId="model.id" :editable="disableAttachment"></tl-attachment>
            <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
                <svg-icon icon-class="list" />审批历史</el-button>
            <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval">
                <svg-icon icon-class="undo" />取消审批</el-button>
        </div>
        <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
        <tlpm-team-ref ref="pmTeamRef" @confirm="getpmTeamRefData" :isSingleSelect="true"></tlpm-team-ref>
        <tlpm-team-ref ref="pmTeamAnalysisRef" @confirm="getpmTeamRefAnalysisData" :isSingleSelect="true"></tlpm-team-ref>
    </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlpmProjectRef from '@/views/ProjectManage/pmProject/refList.vue'
import tlpmTeamRef from '@/views/ProjectManage/pmTeam/refList.vue'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
    mixins: [mixin_approval_edit],
    components: { tlpmProjectRef, tlpmTeamRef },
    data() {
        return {
            moduleTitle: '风险识别',
            urlPrefix: '/pm/riskIdentify/',
            url: {
                // 额外的url
            }
        }
    },
    methods: {
        assignDefaultValue() {
            if (this.isAdd) {
                if (this.codeModel.flowidList.length > 0) {
                    this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
                }
                this.$set(this.model, 'orgcode', this.org.code)
                this.$set(this.model, 'orgname', this.org.name)
                this.$set(this.model, 'deptcode', this.dept.code)
                this.$set(this.model, 'deptname', this.dept.fullname)
                this.$set(this.model, 'usercode', this.user.code)
                this.$set(this.model, 'username', this.user.name)
                this.$set(this.model, 'docdate', new Date())
                this.$set(this.model, 'findtime', new Date())
            }
        },
        loaded() {
            // 页面加载完毕执行
        },
        /** 打开项目参照 */
        openpmProjectRefDialog() {
            this.$refs.pmProjectRef.visible = true
        },
        /** 获取项目参数数据 */
        getpmProjectRefData(selectionRow, selectKey, selectionValue) {
            const { id, name } = selectionRow
            this.$set(this.model, 'projectid', id)
            this.$set(this.model, 'dispprojectid', name)
        },
        /** 打开识别人参照 */
        openpmTeamRefDialog() {
            this.$refs.pmTeamRef.visible = true
        },
        /** 打开风险分析人参照 */
        openpmTeamRefDialogAnalysis() {
            this.$refs.pmTeamAnalysisRef.visible = true
        },
        /** 获取识别人参数数据 */
        getpmTeamRefData(selectionRow, selectKey, selectionValue) {
            this.$set(this.model, 'identifyusername', selectionRow.username)
            this.$set(this.model, 'identifyusercode', selectionRow.usercode)
        },
        /** 获取识别人参数数据 */
        getpmTeamRefAnalysisData(selectionRow, selectKey, selectionValue) {
            this.$set(this.model, 'analysisusername', selectionRow.username)
            this.$set(this.model, 'analysisusercode', selectionRow.usercode)
        },
        saveCourierInfo(resolve, reject) {
            const { id, riskrank, riskstate, probability, influencedegree, measures } = this.model
            if (!riskrank) {
                this.$tap.alert('请填写风险等级')
                reject()
            }
            if (!riskstate) {
                this.$tap.alert('请填写风险状态')
                reject()
            }
            if (!probability) {
                this.$tap.alert('请填写发生概率')
                reject()
            }
            if (!influencedegree) {
                this.$tap.alert('请填写影响程度')
                reject()
            }
            if (!measures) {
                this.$tap.alert('请填写建议措施')
                reject()
            }
            const param = {
                id,
                riskrank,
                riskstate,
                probability,
                influencedegree,
                measures
            }
            putAction(this.url.edit, param)
                .then(res => {
                    if (res.success) {
                        resolve()
                    } else {
                        this.$tap.alert(res.message, 'warning')
                        reject()
                    }
                })
                .catch(err => {
                    this.$tap.alert('保存风险分析信息失败，请联系系统管理员', 'warning')
                    reject()
                })
        }
    }
}
</script>

